package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 查询一个  localhost:8080/employee/findone  1
     * localhost:8080/employee/1
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "员工查找",notes = "根据ID查找员工")
    public Employee findOne(@PathVariable("id") Long id){
        Employee one = employeeService.findOne(id);
        return one;
    }

    /**
     * 查询所有localhost:8080/employee
     * @return
     */
    @GetMapping
    @ApiOperation(value = "员工查找",notes = "查找所有员工")
    public List<Employee> findAll(){
        List<Employee> list = employeeService.findAll();
        return list;
    }
    @PutMapping  /*@RequestBody  接收请求体localhost:8080/employee*/
    @ApiOperation(value = "添加或者修改员工",notes = "添加或者修改员工")
    public AjaxResult addorUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){//添加
                employeeService.add(employee);
            }else{//修改
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/employee/4*/
    @ApiOperation(value = "删除员工",notes = "根据ID删除员工")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PatchMapping  /*@RequestBody  接收请求体localhost:8080/employee*/
    @ApiOperation(value = "批量删除",notes = "根据ids删除员工")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查找员工",notes = "分页查找员工")/*localhost:8080/employee*/
    public PageList<Employee> queryForPage(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> pageList = employeeService.queryForPage(employeeQuery);
        return pageList;
    }
}
